package com.ticket.utils;

public class UserContext {
    private static final ThreadLocal<Long> userIdHolder = new ThreadLocal<>();
    private static final ThreadLocal<String> roleHolder = new ThreadLocal<>();

    public static void setCurrentUserId(Long userId) {
        userIdHolder.set(userId);
    }

    public static Long getCurrentUserId() {
        Long userId = userIdHolder.get();
        if (userId == null) {
            throw new RuntimeException("用户未登录");
        }
        return userId;
    }

    public static void setCurrentRole(String role) {
        roleHolder.set(role);
    }

    public static String getCurrentRole() {
        String role = roleHolder.get();
        if (role == null) {
            throw new RuntimeException("用户未登录");
        }
        return role;
    }

    public static boolean hasRole(String expectedRole) {
        String role = roleHolder.get();
        return role != null && role.equals(expectedRole);
    }

    public static void clear() {
        userIdHolder.remove();
        roleHolder.remove();
    }
}
